<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/wang-cheng/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/wang-cheng/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/wang-cheng/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/wang-cheng/images/logo.svg" color="#222">

<link rel="stylesheet" href="/wang-cheng/css/main.css">


<link rel="stylesheet" href="/wang-cheng/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"cheng-wang123.gitee.io","root":"/wang-cheng/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="排序算法篇插入排序插入排序，一般也称为直接插入排序。 对于少量元素的排序，它是一个有效的算法。 假设前面n-1个数已经是排好顺序的，现将第n个数插到前面已经排好的顺序中，然后找到适合自己的位置，使得插入第n个数的这个序列也是排好顺序的 按照此法对所有元素进行插入，直到整个序列排为有序的过程，称为插入排序 插入排序的平均时间复杂度也是O(n^2)，空间复杂度为常数阶O(1)，具体时间复杂度和数组的有">
<meta property="og:type" content="article">
<meta property="og:title" content="算法专题">
<meta property="og:url" content="https://cheng-wang123.gitee.io/wang-cheng/2022/03/26/%E7%AE%97%E6%B3%95%E4%B8%93%E9%A2%98/index.html">
<meta property="og:site_name" content="诚の博客">
<meta property="og:description" content="排序算法篇插入排序插入排序，一般也称为直接插入排序。 对于少量元素的排序，它是一个有效的算法。 假设前面n-1个数已经是排好顺序的，现将第n个数插到前面已经排好的顺序中，然后找到适合自己的位置，使得插入第n个数的这个序列也是排好顺序的 按照此法对所有元素进行插入，直到整个序列排为有序的过程，称为插入排序 插入排序的平均时间复杂度也是O(n^2)，空间复杂度为常数阶O(1)，具体时间复杂度和数组的有">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cheng-wang123.gitee.io/wang-cheng/2022/03/26/%E7%AE%97%E6%B3%95%E4%B8%93%E9%A2%98/%E5%BD%92%E5%B9%B6%E6%8E%92%E5%BA%8F.png">
<meta property="article:published_time" content="2022-03-26T11:17:51.000Z">
<meta property="article:modified_time" content="2022-03-27T05:58:34.922Z">
<meta property="article:author" content="王诚">
<meta property="article:tag" content="排序算法篇">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cheng-wang123.gitee.io/wang-cheng/2022/03/26/%E7%AE%97%E6%B3%95%E4%B8%93%E9%A2%98/%E5%BD%92%E5%B9%B6%E6%8E%92%E5%BA%8F.png">

<link rel="canonical" href="https://cheng-wang123.gitee.io/wang-cheng/2022/03/26/%E7%AE%97%E6%B3%95%E4%B8%93%E9%A2%98/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>算法专题 | 诚の博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/wang-cheng/atom.xml" title="诚の博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/wang-cheng/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">诚の博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">记录学习中的点点滴滴</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/wang-cheng/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/wang-cheng/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/wang-cheng/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/wang-cheng/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://cheng-wang123.gitee.io/wang-cheng/2022/03/26/%E7%AE%97%E6%B3%95%E4%B8%93%E9%A2%98/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/wang-cheng/images/avatar.gif">
      <meta itemprop="name" content="王诚">
      <meta itemprop="description" content="命运对勇士低语，你无法抵御风暴；勇士低声回应，我就是风暴">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="诚の博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          算法专题
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-03-26 19:17:51" itemprop="dateCreated datePublished" datetime="2022-03-26T19:17:51+08:00">2022-03-26</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-03-27 13:58:34" itemprop="dateModified" datetime="2022-03-27T13:58:34+08:00">2022-03-27</time>
              </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="排序算法篇"><a href="#排序算法篇" class="headerlink" title="排序算法篇"></a><strong>排序算法篇</strong></h1><h2 id="插入排序"><a href="#插入排序" class="headerlink" title="插入排序"></a><strong>插入排序</strong></h2><p>插入排序，一般也称为直接插入排序。</p>
<p>对于少量元素的排序，它是一个有效的算法。</p>
<p>假设前面n-1个数已经是排好顺序的，现将第n个数插到前面已经排好的顺序中，然后找到适合自己的位置，使得插入第n个数的这个序列也是排好顺序的</p>
<p>按照此法对所有元素进行插入，直到整个序列排为有序的过程，称为插入排序</p>
<p>插入排序的平均时间复杂度也是O(n^2)，空间复杂度为常数阶O(1)，具体时间复杂度和数组的有序性也是有关联的。最优情况的时间复杂度为O(n)，最坏情况时间复杂度为O(n^2)</p>
<p>从小到大的插入排序算法</p>
<figure class="highlight abnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">public class InsertionSort &#123;</span><br><span class="line">    public static void sort(int[] arr)&#123;</span><br><span class="line">        int n <span class="operator">=</span> arr.length<span class="comment">;</span></span><br><span class="line">        int temp <span class="operator">=</span> <span class="number">0</span><span class="comment">;</span></span><br><span class="line">        for(int i <span class="operator">=</span> <span class="number">0</span><span class="comment">;i &lt; n; i++)&#123;</span></span><br><span class="line">            //寻找元素arr[i]合适的插入位置</span><br><span class="line">            for(int j <span class="operator">=</span> i<span class="comment">;j &gt; 0;j--)&#123;</span></span><br><span class="line">                if(arr[i] &lt; arr[i-<span class="number">1</span>])&#123;</span><br><span class="line">                    temp <span class="operator">=</span> arr[i-<span class="number">1</span>]<span class="comment">;</span></span><br><span class="line">                    arr[i-<span class="number">1</span>] <span class="operator">=</span> arr[i]<span class="comment">;</span></span><br><span class="line">                    arr[i] <span class="operator">=</span> temp<span class="comment">;</span></span><br><span class="line">                &#125;else</span><br><span class="line">                    berak<span class="comment">;</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="希尔排序"><a href="#希尔排序" class="headerlink" title="希尔排序"></a><strong>希尔排序</strong></h2><p>希尔排序是插入排序的一种，它是针对直接插入排序算法的改进</p>
<p>希尔排序又称缩小增量排序，它通过比较相距一定间隔的元素来进行，各躺比较所用的距离随着算法的进行而减小，直到只比较相邻元素的最后一趟排序为止。</p>
<p>希尔排序时间复杂度是O(n^(1.3-2))，空间复杂度为常数阶O(1)。希尔排序没有时间复杂度为O(n(logn)) 的快速排序算法快，因此对中等大小规模表现良好，但对规模非常大的数据排序不是最优选择，总之比一般O(n^2 )复杂度的算法快得多。</p>
<figure class="highlight gradle"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> ShellSort&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="keyword">sort</span>(<span class="keyword">int</span>[] arr)&#123;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> gap = arr.length<span class="regexp">/2; gap &gt; 0;gap /</span>= <span class="number">2</span>)&#123;</span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i = gap; i &lt; arr.length; i++)&#123;</span><br><span class="line">                <span class="keyword">int</span> tmp = arr[i];</span><br><span class="line">                <span class="keyword">for</span>(<span class="keyword">int</span> j = i; j &gt;= gap &amp;&amp; tmp &lt; arr[j-gap]; j -= gap)&#123;</span><br><span class="line">                    arr[j] = arr[j- gap];</span><br><span class="line">                &#125;</span><br><span class="line">                arr[j] = tmp;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>个人理解：</p>
<p>第一个for循环是逐步缩小增量</p>
<p>第二个for循环是依次从第一个增量处开始遍历整个数组</p>
<p>tmp的记录i下标的值，这个值是可能需要调整的值，所以用tmp暂存下来。当调整的时候只需要将比这个值大的数放到这个下标所在的位置就可以了</p>
<p>第三个for循环是对tmp所记录的值进行比较，所比较的数是从第i个数开始向后遍历存在于这个增量上的数。</p>
<p>arr[j] &#x3D; tmp是在比较完成后，再将tmp暂存的值赋值给j所在下标的位置(如果上一步没有进行调整，则tmp与arr[j]的值是相同的，也就是说又将i下标的值放回原处了)</p>
<h2 id="选择排序"><a href="#选择排序" class="headerlink" title="选择排序"></a><strong>选择排序</strong></h2><p>选择排序是一种简单直观的排序算法。其基本思想是:首先在未排序的数列中找到最小(大)元素，然后将其存放到数列的起始位置；接着，再从剩余未排序的元素中继续寻找最小(大)元素，然后放到已排序序列的末尾。以此类推，直到所有元素均排序完毕</p>
<figure class="highlight excel"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">void select_sort(<span class="built_in">int</span>[] a,<span class="built_in">int</span> <span class="built_in">n</span>)&#123;</span><br><span class="line">    <span class="built_in">int</span> i;</span><br><span class="line">    <span class="built_in">int</span> j;</span><br><span class="line">    <span class="built_in">int</span> <span class="built_in">min</span>;</span><br><span class="line">    for(i = <span class="number">0</span>; i &lt; <span class="built_in">n</span>; i++)&#123;</span><br><span class="line">        <span class="built_in">min</span> = i;</span><br><span class="line">        for(j = i+<span class="number">1</span>;j &lt; <span class="built_in">n</span>;j++)&#123;</span><br><span class="line">            <span class="built_in">if</span>(a[j] &lt; a[<span class="built_in">min</span>])&#123;</span><br><span class="line">                <span class="built_in">min</span> = j;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">if</span>(<span class="built_in">min</span> != i)&#123;</span><br><span class="line">            <span class="built_in">int</span> temp = a[i];</span><br><span class="line">            a[i] = a[<span class="built_in">min</span>];</span><br><span class="line">            a[<span class="built_in">min</span>] = temp;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="冒泡排序"><a href="#冒泡排序" class="headerlink" title="冒泡排序"></a><strong>冒泡排序</strong></h2><p>冒泡排序就是每遍历一轮，都让最大(小)的数交换到最右(左)边，因为思路很简单，就不多说了，直接上代码</p>
<figure class="highlight inform7"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">void BubbleSort(int a<span class="comment">[]</span>, int len)&#123;</span><br><span class="line">    int i,j,temp;</span><br><span class="line">    for(j = 0;j &lt; len-1;j++)&#123;</span><br><span class="line">        for(i = 0; i &lt;len-1-j; i++)&#123;</span><br><span class="line">            if(a<span class="comment">[i]</span>&gt;a<span class="comment">[i+1]</span>)&#123;</span><br><span class="line">                temp = a<span class="comment">[i]</span>;</span><br><span class="line">                a<span class="comment">[i]</span> = a<span class="comment">[i+1]</span>;</span><br><span class="line">                a<span class="comment">[i+1]</span> = temp;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h2 id="快速排序"><a href="#快速排序" class="headerlink" title="快速排序"></a><strong>快速排序</strong></h2><p>快速排序由于排序效率在同为O(N*logN)的几种排序算法中效率较高，因此经常被采用。</p>
<p>原理：快速排序的基本思想，是通过一趟排序将待排记录分隔成独立的两部分，其中一部分记录的关键字均比另一部分的关键字小，则可分别对这两部分记录继续进行排序，以达到整个序列有序。说白了就是给基准数据找其正确索引位置的过程。</p>
<p>基本思想：</p>
<ul>
<li><p>先从数列中取出一个数作为基准数</p>
</li>
<li><p>分区过程，将比这个数大的数全部放到它的右边，小于或等于它的数全部放到它的左边</p>
</li>
<li><p>再对左右区间重复第二步，直到各区间只有一个数</p>
</li>
</ul>
<figure class="highlight reasonml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">void quick<span class="constructor">Sort(<span class="params">int</span>[] <span class="params">arr</span>,<span class="params">int</span> <span class="params">low</span>,<span class="params">int</span> <span class="params">high</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(low &lt; high)&#123;</span><br><span class="line">        <span class="built_in">int</span> index = get<span class="constructor">Index(<span class="params">arr</span>,<span class="params">low</span>,<span class="params">high</span>)</span>;</span><br><span class="line">        quick<span class="constructor">Sort(<span class="params">arr</span>,<span class="params">low</span>,<span class="params">index</span>-1)</span>;</span><br><span class="line">        quick<span class="constructor">Sort(<span class="params">arr</span>,<span class="params">index</span>+1,<span class="params">high</span>)</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">int</span> get<span class="constructor">Index(<span class="params">int</span>[] <span class="params">arr</span>,<span class="params">int</span> <span class="params">low</span>,<span class="params">int</span> <span class="params">high</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">int</span> temp = arr<span class="literal">[<span class="identifier">low</span>]</span>;</span><br><span class="line">    <span class="keyword">while</span>(low &lt; high)&#123;</span><br><span class="line">        <span class="keyword">while</span>(low &lt; high<span class="operator"> &amp;&amp; </span>arr<span class="literal">[<span class="identifier">high</span>]</span> &gt;= temp)&#123;</span><br><span class="line">            high--;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(low &lt; high)&#123;</span><br><span class="line">            arr<span class="literal">[<span class="identifier">low</span>]</span> = arr<span class="literal">[<span class="identifier">high</span>]</span>;</span><br><span class="line">            low++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">while</span>(low &lt; high<span class="operator"> &amp;&amp; </span>arr<span class="literal">[<span class="identifier">low</span>]</span> &lt; = temp)&#123;</span><br><span class="line">            low++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(low &lt; high)&#123;</span><br><span class="line">            arr<span class="literal">[<span class="identifier">high</span>]</span> arr<span class="literal">[<span class="identifier">low</span>]</span>;</span><br><span class="line">            high--;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    arr<span class="literal">[<span class="identifier">low</span>]</span> = temp;</span><br><span class="line">    return low;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="归并排序"><a href="#归并排序" class="headerlink" title="归并排序"></a><strong>归并排序</strong></h2><p>归并排序是建立在归并操作上的一种有效、稳定的排序算法，该算法是采用分冶法的一个非常典型的应用。将已有序的子序列合并，得到完全有序的序列。即先使每个子序列有序，再使子序列段间有序。若将两个有序表合并成一个有序表，称为二路归并</p>
<p>归并排序的时间复杂度为O(nlogn)。归并排序时需要和待排序记录个数相等的存储空间，所以空间复杂度为O(n)。归并排序适用于数据量大，并且对稳定性有要求的场景。</p>
<img src="/wang-cheng/2022/03/26/%E7%AE%97%E6%B3%95%E4%B8%93%E9%A2%98/%E5%BD%92%E5%B9%B6%E6%8E%92%E5%BA%8F.png" class title="归并排序">

<figure class="highlight pgsql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">public</span> <span class="type">void</span> sort(<span class="type">int</span>[] arr)&#123;</span><br><span class="line">    <span class="type">int</span>[] <span class="keyword">temp</span> = <span class="built_in">new</span> <span class="type">int</span>[arr.length];</span><br><span class="line">    sort(arr,<span class="number">0</span>,arr.length<span class="number">-1</span>,<span class="keyword">temp</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">public</span> <span class="type">void</span> sort(<span class="type">int</span>[] arr,<span class="type">int</span> left,<span class="type">int</span> right,<span class="type">int</span>[] <span class="keyword">temp</span>)&#123;</span><br><span class="line">    <span class="keyword">if</span>(left &lt; right)&#123;</span><br><span class="line">        <span class="type">int</span> mid = (left+right)/<span class="number">2</span>;</span><br><span class="line">        sort(arr,left,mid,<span class="keyword">temp</span>);</span><br><span class="line">        sort(arr,mid+<span class="number">1</span>,right,<span class="keyword">temp</span>);</span><br><span class="line">        merge(arr,left,mid,right,<span class="keyword">temp</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">public</span> <span class="type">void</span> merge(<span class="type">int</span>[] arr,<span class="type">int</span> left,<span class="type">int</span> mid,<span class="type">int</span> right,<span class="type">int</span>[] <span class="keyword">temp</span>)&#123;</span><br><span class="line">    <span class="type">int</span> i = left;   //左序列指针</span><br><span class="line">    <span class="type">int</span> j = mid+<span class="number">1</span>;  //右序列指针</span><br><span class="line">    <span class="type">int</span> t = <span class="number">0</span>;      //临时数组指针</span><br><span class="line">    <span class="keyword">while</span>(i &lt;= mid &amp;&amp; j &lt;= right)&#123;</span><br><span class="line">        <span class="keyword">if</span>(arr[i] &lt;= arr[j])&#123;</span><br><span class="line">            <span class="keyword">temp</span>[t++] = arr[i++];</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">temp</span>[t++] = <span class="keyword">temp</span>[j++];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span>(i &lt;= mid)&#123;    //将左边剩余元素填充进<span class="keyword">temp</span>中</span><br><span class="line">        <span class="keyword">temp</span>[i++] = arr[i++]</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span>(j &lt;= right)&#123;      //将右序列剩余元素填充进<span class="keyword">temp</span>中</span><br><span class="line">        <span class="keyword">temp</span>[i++] arr[j++]</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    t = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    //将<span class="keyword">temp</span>中的元素全部拷贝到原数组中</span><br><span class="line">    <span class="keyword">while</span>(left &lt;= right)&#123;</span><br><span class="line">        arr[left++] = <span class="keyword">temp</span>[t++];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

































    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/wang-cheng/tags/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95%E7%AF%87/" rel="tag"># 排序算法篇</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/wang-cheng/2022/03/26/%E5%81%9A%E9%A2%98%E7%AC%94%E8%AE%B0-%E4%BA%8C/" rel="prev" title="做题笔记(二)">
      <i class="fa fa-chevron-left"></i> 做题笔记(二)
    </a></div>
      <div class="post-nav-item">
    <a href="/wang-cheng/2022/03/31/%E7%AE%97%E6%B3%95%E7%AC%94%E8%AE%B0-%E4%B8%80/" rel="next" title="算法笔记(一)">
      算法笔记(一) <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95%E7%AF%87"><span class="nav-number">1.</span> <span class="nav-text">排序算法篇</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F"><span class="nav-number">1.1.</span> <span class="nav-text">插入排序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F"><span class="nav-number">1.2.</span> <span class="nav-text">希尔排序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%80%89%E6%8B%A9%E6%8E%92%E5%BA%8F"><span class="nav-number">1.3.</span> <span class="nav-text">选择排序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F"><span class="nav-number">1.4.</span> <span class="nav-text">冒泡排序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F"><span class="nav-number">1.5.</span> <span class="nav-text">快速排序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BD%92%E5%B9%B6%E6%8E%92%E5%BA%8F"><span class="nav-number">1.6.</span> <span class="nav-text">归并排序</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">王诚</p>
  <div class="site-description" itemprop="description">命运对勇士低语，你无法抵御风暴；勇士低声回应，我就是风暴</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/wang-cheng/archives/">
        
          <span class="site-state-item-count">32</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/wang-cheng/tags/">
          
        <span class="site-state-item-count">12</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">王诚</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/wang-cheng/lib/anime.min.js"></script>
  <script src="/wang-cheng/lib/velocity/velocity.min.js"></script>
  <script src="/wang-cheng/lib/velocity/velocity.ui.min.js"></script>

<script src="/wang-cheng/js/utils.js"></script>

<script src="/wang-cheng/js/motion.js"></script>


<script src="/wang-cheng/js/schemes/pisces.js"></script>


<script src="/wang-cheng/js/next-boot.js"></script>




  















  

  

  

</body>
</html>
